<?php

/**
 * Created by PhpStorm.
 * User: panya
 * Date: 16/4/16
 * Time: 上午11:08
 * @property string table_name
 * @property string chinese_name
 */
class team extends CI_Controller
{
    function __construct () {
        parent::__construct();
        $this->table_name = 'team';
        $this->chinese_name = '战队';
    }
    function index()
    {
        $this -> pub -> logp('浏览' . $this->chinese_name, '1-2-3');
        $da = $this -> pub -> get_setting($this->chinese_name, 'Team');

        $this -> load -> view('adminx/team', $da);
    }
    // 添加战队
    function add() {
        $this -> pub -> logp('添加战队', '1-2-3');

        $result['msg'] = '';
        if ($_POST) {
            // 添加
            $da['name'] = $this -> input -> post('name');
            $da['leader'] = $this -> input -> post('leader');
            $da['head_image'] = $this -> input -> post('head_image');
            $da['mk'] = time();

            // ---查询新队长是否有在其他队当队长--- //
            $q = $this->db->select('team_id, team_level, nickname')
                ->where_in('mid', $da['leader'])
                ->get('member');
            if ($q->row_array(0)['team_level'] == '1') {
                // 已经做了队长
                $result['type'] = 2;
                $result['msg'] = $q->row_array(0)['nickname'] . '在其他队已经是队长,不能再添加!';
            } else {
                $this -> pub -> insert($this->table_name, $da);
                $result['idx'] = $this -> db -> insert_id();
                $result['msg'] = '添加'.$this->chinese_name.'成功！';
                $result['type'] = 1;
                // ---更新新队长等级信息START--- //
                $this -> db -> update('member', array(
                    'team_id' => $result['idx'],
                    'team_level' => 1
                ), array('mid' => $da['leader']));
                // ---更新新队长等级信息END--- //
                $this -> pub -> logx('添加'.$this->chinese_name.' - ' . $da['name']);
            }

            echo json_encode($result);
        } else {
            $da = $this -> pub -> get_setting($this->chinese_name, 'Team');
            $da['action'] = '添加';
            $this -> load -> view('adminx/team_add.php', $da);
        }
    }
    // 获取战队
    function get() {
        $this -> pub -> logp('获取'.$this->chinese_name.'信息 ', '1-2-3');

        $this -> db -> where('team_del', '1');
        if ($this -> input -> post('key')) {
            $this -> db -> select('count(*)');

            $this -> db -> like('name', $this -> input -> post('key'));
            $q = $this -> db -> get_where($this->table_name);
            $ls = $q -> row_array(0);
            $num = $ls['count(*)'];
        } else {
            $this -> db -> select('count(*)');
            $q = $this -> db -> get_where($this->table_name);
            $ls = $q -> row_array(0);
            $num = $ls['count(*)'];
        }
        // 一共的页数
        $da['num'] = ceil($num / 30);
        // 当前页数
        $page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
        $da['page'] = $page;

        $page = ($page - 1) == -1 ? 0 : ($page - 1);
        $this -> db -> limit(30, $page * 30);

        $q = $this -> db -> select('team.*, member.nickname')
            -> join('member', 'member.mid = team.leader', 'left')
            -> get($this->table_name);
        $result = $q->result_array();
        foreach ($result as $key => $value) {
            $value['head_image'] = $this -> pub -> oss_dz($value['head_image']);
            $result[$key] = $value;
        }

        header('Content-type:text/json');
        echo json_encode($result);
    }
    // 更新战队
    function up($idx = 0){
        $this -> pub -> logp('修改'.$this->chinese_name.' - ' . $idx, '1-2-3');
        if ($_POST) {
            // 更新操作
            $da['name'] = $this -> input -> post('name');
            $da['leader'] = $this -> input -> post('leader');
            $da['head_image'] = $this -> input -> post('head_image');


            // ---查询新队长是否有在其他队当队长--- //
            $q = $this->db->select('team_id, team_level, nickname')
                ->where_in('mid', $da['leader'])
                ->get('member');
            if ($q->row_array(0)['team_level'] == '1') {
                // 已经做了队长
                $result['type'] = 2;
                $result['msg'] = $q->row_array(0)['nickname'] . '在其他队已经是队长,不能再添加!';
            } else {
                // ---修改老队长的等级信息START--- //
                $q = $this -> db -> select('leader')
                    -> where_in('team_id', $idx)
                    -> get($this->table_name);
                $old = $q -> row_array(0);
                $this -> db -> update('member', array(
                    'team_level' => 9
                ), array('mid' => $old['leader']));
                // ---修改老队长的等级信息END--- //

                // ---更新新队长等级信息START--- //
                $this -> db -> update('member', array(
                    'team_id' => $idx,
                    'team_level' => 1
                ), array('mid' => $da['leader']));
                // ---更新新队长等级信息END--- //

                $this -> db -> update($this->table_name, $da, array('team_id' => $idx));
                $this -> pub -> logx('修改'.$this->chinese_name.' - ' . $da['name']);
                $result['type'] = 1;
            }
            echo json_encode($result);
        } else {
            // 渲染界面
            $q = $this -> db -> select('team.*, member.nickname')
                -> where_in('team.team_id', $idx)
                -> join('member', 'member.mid = team.leader', 'left')
                -> get($this->table_name);
            if ($q -> num_rows() > 0) {
                $da = $this -> pub -> get_setting('战队', 'Team');
                $da['data'] = $q -> row_array(0);
                $da['action'] = '修改';
                
                $this -> load -> view('adminx/team_add', $da);
            } else {
                show_error('该战队不存在！');
            }
        }
    }
    // 删除战队
    function del()
    {
        $this->pub->logp('删除'.$this->chinese_name.' - ' . $this->input->post('idx'), '1-2-3');
        $ids = explode(",", $this->input->post('idx'));
        $this->db->where_in('team_id', $ids);
        $q = $this -> db -> get_where($this->table_name);
        if ($q -> num_rows() > 0) {
            $this -> db -> update('member', array(
                'team_id' => 0,
                'team_level' => 9
            ), array('mid' => $q->row_object()->leader));
            $this->db->where_in('team_id', $ids);
            $this->db->delete($this->table_name);
            $da['type'] = 1;
            $this -> pub -> logx('删除'.$this->chinese_name.' - ' . $this -> input -> post('idx'));
        } else {
            $da['type'] = 2;
        }
        echo json_encode($da);
    }
    // 根据key获取用户
    function get_leander_user () {
        $key = $this->input->get('key');
        $this -> db -> like('nickname', $key);
        $q = $this -> db -> get_where('member');
        $result = $q->result_array();

        header('Content-type:text/json');
        echo json_encode($result);
    }
}